package com.eshipenet.shipnet.equipmentextbuilder

import play.api.libs.json.{JsDefined, JsNull, JsObject, JsString, JsValue, Json}

import scala.util.Try

object EquipmentModelLink {

  def apply(raw: JsValue): Try[EquipmentModelLink] = Try {
    val eid = (raw \ "eid").as[String]
    val mid = (raw \ "mid").as[String]
    val dataBinding = raw \ "dataBinding" match {
      case JsDefined(JsString(s)) => Json.parse(s).as[Map[String, String]]
      case JsDefined(x: JsObject) => x.as[Map[String, String]]
      case JsDefined(JsNull)      => Map[String, String]()
      case _                      => throw new Exception("invalid dataBinding")
    }
    new EquipmentModelLink(eid, mid, dataBinding, raw)
  }

}

case class EquipmentModelLink private (eid: String, mid: String, dataBinding: Map[String, String], rawObj: JsValue) {
  val raw: JsValue = rawObj
}
